package com.hw.base.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.hw.R;
import com.hw.base.model.Model;
import com.hw.base.mvp.BaseMvp;
import com.hw.base.present.BasePresenter;
import com.hw.base.view.BaseView;
import com.hw.broadcast.NetWorkChangReceiver;
import com.hw.dialog.WaitDialog;
import com.hw.scan.BroadcastConfig;
import com.hw.tool.ToastUtils;
import java.io.Serializable;

public abstract class BaseActivity<M extends Model, V extends BaseView, P extends BasePresenter> extends Activity implements BaseMvp<M, V, P>, BaseView {
    protected String tag = "BaseActivity";
    protected WaitDialog waitDialog;
    protected P presenter;
    protected Context mContext;
    protected NetWorkChangReceiver netWorkChangReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        waitDialog = new WaitDialog(this, R.style.Dialog1);
        WindowManager.LayoutParams lp = waitDialog.getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        mContext= this;
        //创建
        presenter = createPresenter();
        if (presenter != null) {
            //将Model层注册到Presenter中
            presenter.registerModel(createModel());
            //将View层注册到Presenter中
            presenter.registerView(createView());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void showProgress() {
        waitDialog.show();
    }

    @Override
    public void hideProgress() {
        if (waitDialog != null) {
            waitDialog.dismiss();
        }
    }

    @Override
    public void showMsg(String msg) {
        ToastUtils.showSingleToast(this, msg);
    }

    protected void intentToActivityWithoutParameter(Activity context, Class clazz) {
        Intent intent = new Intent();
        intent.setClass(context, clazz);
        startActivity(intent);
    }

    protected void intentToActivityWithSParameter(Activity context, Class clazz, String key, Serializable object) {
        Intent intent = new Intent();
        intent.putExtra(key, object);
        intent.setClass(context, clazz);
        startActivity(intent);
    }

    protected void initBroadcastReciever() {
        // 发送广播到扫描工具内的应用设置项
        Intent intent = new Intent(BroadcastConfig.BROADCAST_SETTING);
        // 修改扫描工具内应用设置中的开发者项下的广播名称
        intent.putExtra(BroadcastConfig.BROADCAST_KEY, BroadcastConfig.CUSTOM_NAME);
//        intent.putExtra(BroadcastConfig.SEND_KEY, "BROADCAST");
//        intent.putExtra(BroadcastConfig.END_KEY, "NONE");
        intent.putExtra(BroadcastConfig.VIBERATE_KEY, false);
        intent.putExtra(BroadcastConfig.SOUND_KEY, true);
        intent.putExtra(BroadcastConfig.CONTINIU_KEY, false);
//        intent.putExtra("interval", 1000);
        sendBroadcast(intent);
    }
}
